﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

namespace HobSoft.SoccerBet2014.Domain.Implementation.SqlRepository
{
    using Domain.IRepositories;
    using Domain.Models;
    using SoccerBet2014.Common.DB.Queryable;

    public class BookmakerRepository : ReadWriteSqlEntRepository<Bookmaker>, IBookmakerRepository
    {
        public BookmakerRepository(DbContext dbContext)
            : base(dbContext)
        {
            
        }

        public override Bookmaker Get(params object[] id)
        {
            if (id.Length == 0) return null;
            int bmId = (int)id[0];
            var bm=DbSet.Find(id);
            DbContext.Entry(bm).Collection(b => b.Accounts).Load();
            return bm;
        }

        public override IList<Bookmaker> GetMany(Expression<Func<Bookmaker, bool>> predicate)
        {
            if (predicate == null)
                throw new ArgumentNullException("predicate", "need predicate.");
            
            return DbSet.Include(b => b.Accounts)
                        .Where(predicate)
                        .ToList();
        }

        public override IList<Bookmaker> GetMany(Expression<Func<Bookmaker, bool>> predicate, params SoccerBet2014.Common.Queryable.ISortExpression<Bookmaker>[] sortExpression)
        {
            if (predicate == null)
                throw new ArgumentNullException("predicate", "need predicate.");
            if (sortExpression == null)
                throw new ArgumentNullException("sortExpression", "need sortExpression.");

            return DbSet.Include(b => b.Accounts).Where(predicate).Sort(sortExpression).ToList();
        }
    }
}
